/*
选择服务器
*/

layui.define(['form','cosjs','scene'],function (exports) {
    let $ = layui.jquery,form = layui.form,cosjs = layui.cosjs,scene = layui.scene;

    function select() {
        let data = {};
        data['server'] = this.gamesdk.gzone.server.get();
        return data;
    }
    select.finish = function finish(data){
        let gzone = scene.gamesdk.gzone.account.gzone;
        if (gzone && gzone.length > 0) {
            $("#sid").val(gzone[0]);
        }
        form.on('submit(gzone-select-submit)', function (data) {
            let sid = parseInt(data.field.sid);
            if (!sid) {
                return scene.error("请选择服务器")
            }

            let _curUser=layui.storage('curUser');
            _curUser.set('zoneSelectData',sid);

            console.log("this is data-------:",data)
            console.log("this is sid--------1:",sid)
            scene.gamesdk.gzone.select(sid).then(() => {
                console.log("this is sid--------2:",sid)
                return scene.gamesdk.game.login()
            }).then(roles => {
                console.log("this is sid--------2:",roles)
                if (roles.length > 0) {
                    scene.show('game/select');
                }
                else {
                    scene.show('game/create');
                }
            }).catch(err => {
                console.log("this is err--------2:",err)
                return scene.error(err);
            })
            return false;
        })
    }


    exports('gzone/select', select);
})